package com.modeler.single_flowable_modeler.config;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.Model;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 使用的依赖是 flowable-engine,需要手动注册8大服务
 * 这里仅仅部署放到这个项目就只注册
 * 使用 flowable-spring-boot-starter 依赖，会自动帮忙注册好，不需要自己再注册，直接使用即可
 */
@Slf4j
@Configuration
public class FlowableService {
    @Autowired
    ApplicationContext applicationContext;

    @Bean("processEngine")
    public ProcessEngine processEngine(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
        return springProcessEngineConfiguration.buildProcessEngine();
    }


    //八大接口
    // 业务流程的定义相关服务
    @Bean
    public RepositoryService repositoryService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    // 流程对象实例相关服务
    @Bean
    public RuntimeService runtimeService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

    // 流程任务节点相关服务
    @Bean
    public TaskService taskService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    // 流程历史信息相关服务
    @Bean
    public HistoryService historyService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }

    // 表单引擎相关服务
    @Bean
    public FormService formService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getFormService();
    }

    // 用户以及组管理相关服务
    @Bean
    public IdentityService identityService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

    // 管理和维护相关服务
    @Bean
    public ManagementService managementService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }

    // 动态流程服务
    @Bean
    public DynamicBpmnService dynamicBpmnService(@Qualifier("processEngine") ProcessEngine processEngine) {
        return processEngine.getDynamicBpmnService();
    }
    //八大接口 end

    @PostConstruct
    public void deploy(){
        RepositoryService repositoryService = applicationContext.getBean(RepositoryService.class);
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("fff.bpmn20.xml")
                .deploy();
        log.info("流程部署完毕");

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
        System.out.println("Found process definition : " + processDefinition.getName() + processDefinition.getId());


        RuntimeService runtimeService = applicationContext.getBean(RuntimeService.class);
        TaskService taskService = applicationContext.getBean(TaskService.class);

        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("fff");
        String instanceId = processInstance.getId();
        Task task1 = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        taskService.complete(task1.getId());




        List<ProcessInstance> test_process = runtimeService.createProcessInstanceQuery().processDefinitionId(processDefinition.getId()).orderByProcessDefinitionId().desc().list();

        for (ProcessInstance processInst : test_process){
            log.info("流程实例[{}]下任务如下：", processInst.getId());

            List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInst.getId()).active().list();
            System.out.println("You have " + tasks.size() + " tasks:");
            for (int i=0; i<tasks.size(); i++) {
                Task task = tasks.get(i);
                String processInstanceId = task.getProcessInstanceId();
                String name = task.getName();
                String id = task.getId();
                log.info("任务信息如下：流程实例ID：[{}], 任务名:[{}], 任务id:[{}],流程定义ID：[{}]", processInstanceId, name, id, task.getProcessDefinitionId());
                if (name.equals("行领导审批")){
                    taskService.complete(id);
                }
            }
        }

        Model model = repositoryService.createModelQuery().deploymentId(deployment.getId()).singleResult();
        System.out.println(model);


    }

}
